package com.dxf.redis.distributedlock;

import com.dxf.redis.pubsub.CustomJedisPubSub;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.UUID;

public class SharedTra {


    private static final Logger log = LoggerFactory.getLogger(SharedTra.class);
   // private static final RedisLock lock = new RedisLock();
    private static final Lock lock = new ReentrantRedisLock();


    public synchronized void  handle(){

        String uuid =UUID.randomUUID().toString().replaceAll("-", "");
        try{

            boolean flag = lock.lock(uuid, 1000000);
            if(flag){
                log.info("上的分布式锁：{},当前时间：{}",Thread.currentThread().getName(),System.currentTimeMillis());
                this.fun(uuid);
                Thread.sleep(20000);
                lock.unlock(uuid);
                log.info("解锁分布式锁！！！");
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }


    private void fun(String uuid){

        try {
            lock.lock(uuid, 1000000);
            log.info("检查是否是可重入锁!!!!!");
            Thread.sleep(20000);
            lock.unlock(uuid);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }
}
